/**
 * Created by rockyren on 14/12/2.
 */
describe('Unit: common路由测试',function(){
  beforeEach(module('commonServices'));
  var $httpBackend;
  beforeEach(inject(function(_$httpBackend_){
    $httpBackend = _$httpBackend_;
  }));

  /**
   * 测试crud服务
   * @param baseUrl: 请求的基地址
   * @param crudServices: 要测试的服务
   * @returns {{checkGet: Function, checkShow: Function, checkStore: Function, checkUpdate: Function, checkDestroy: Function}}
   *          返回测试的方法对象
   */
  var checkServices = function(baseUrl,crudServices) {

    var requestSpec = {
      checkGet: function () {
        $httpBackend.expectGET(baseUrl).respond(200);
        crudServices.get();
        $httpBackend.flush();
      },
      checkShow: function () {
        $httpBackend.expectGET(baseUrl + '/1').respond(200);
        crudServices.show(1);
        $httpBackend.flush();

      },
      checkStore: function(){
        var postData = {
          status: 'success'
        };
        $httpBackend.expectPOST(baseUrl,postData).respond(200);
        crudServices.store(postData);
        $httpBackend.flush();
      },
      checkUpdate: function(){
        var postData = {
          status: 'success'
        };

        $httpBackend.expectPUT(baseUrl + '/1',postData).respond(200);
        crudServices.update(1,postData);
        $httpBackend.flush();
      },
      checkDestroy: function(){
        $httpBackend.expectDELETE(baseUrl + '/1').respond(200);
        crudServices.destroy(1);
        $httpBackend.flush();
      }
    };

    return requestSpec;

  };

  describe('CRUD cart services',function(){
    var cartServices,requestSpec;
    beforeEach(inject(function(_cartServices_){
      cartServices = _cartServices_;
      requestSpec = checkServices('api/cart',cartServices);

    }));

    it('应该能请求购物车列表',function(){
      requestSpec.checkGet();
    });

    it('应该能请求创建一个商品到购物车',function(){
      requestSpec.checkStore();
    });

    it('应该能请求修改购物车',function(){
      requestSpec.checkUpdate();
    });

    it('应该能请求删除购物车',function(){
      requestSpec.checkDestroy();
    });
  });

  describe('CRUD personal address services',function(){
    var addressServices,requestSpec;
    beforeEach(inject(function(_addressServices_){
      addressServices = _addressServices_;
      requestSpec = checkServices('api/personal/address',addressServices);
    }));

    it('应该能请求到地址列表',function(){
      requestSpec.checkGet();
    });

    it('应该能请求到一个地址的信息',function(){
      requestSpec.checkShow();
    });

    it('应该能请求创建一个地址',function(){
      requestSpec.checkStore();
    });

    it('应该能请求修改一个地址',function(){
      requestSpec.checkUpdate();
    });

    it('应该能删除一个地址',function(){
      requestSpec.checkDestroy();
    });
  });


  describe('district services',function(){
    var districtServices;
    beforeEach(inject(function(_districtServices_){
      districtServices = _districtServices_;
    }));

    it('应该能请求到省份数据',function(){

      $httpBackend.expectGET('api/district').respond(200);
      districtServices.getProvince();

      $httpBackend.flush();
    });

    it('应该能请求到市数据',function(){
      $httpBackend.expectGET('api/district/10').respond(200);
      districtServices.getCity(10);
      $httpBackend.flush();
    });

    it('应该能请求到区数据',function(){
      $httpBackend.expectGET('api/district/10/20').respond(200);
      districtServices.getRegion(10,20);
      $httpBackend.flush();
    });

  });

  describe('order services', function(){
    var orderServices, requestSpec;
    beforeEach(inject(function(_orderServices_){
      orderServices = _orderServices_;
      requestSpec = checkServices('api/order', orderServices);
    }));

    it('check order index', function(){
      requestSpec.checkGet();
    });

    it('check order show', function(){
      requestSpec.checkShow();
    });
    it('check order store', function(){
      requestSpec.checkStore();
    });
    it('check order update', function(){
      requestSpec.checkUpdate();
    });
    it('check order destroy', function(){
      requestSpec.checkDestroy();
    })

  });

});